package com.wws.family.adapter

import android.app.Activity
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import com.wika.basics.base.BaseBindViewHolder
import com.wika.basics.base.BaseRecyclerViewAdapter
import com.wika.basics.ext.toColorSpan
import com.wika.basics.ext.toSizeSpan
import com.wws.family.R
import com.wws.family.databinding.ItemNoteBookBinding
import com.wws.family.db.NoteBookBean
import com.wws.family.helper.ItemTouchHelperAdapter
import com.wws.family.utils.DateUtils
import java.util.Collections


/**
 *@Description:纪念日
 *@Date: 2025/1/7 22:28
 *@Author: WangWeiShuo
 */
class NoteBookAdapter(val context: Activity):BaseRecyclerViewAdapter<NoteBookBean,ItemNoteBookBinding>(),ItemTouchHelperAdapter {
    private var deleteCallBack: OnDeleteCallBack? = null
    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ItemNoteBookBinding>,
        item: NoteBookBean?,
        position: Int
    ) {
        holder.binding.tvCommName.text = item?.name
        val time = item?.time
        val week = DateUtils.obtainWeek(time,DateUtils.YYYYMMDD)
        holder.binding.tvCommTime.text = time + "(${week})"
        holder.binding.tvCommRemark.text = item?.remark

        val remainDay = DateUtils.getBetweenTime(DateUtils.getNowDate(),time)
        if(remainDay > 0) {
            val day = "剩余${remainDay}天"
            val daySpanSize = day.toSizeSpan(IntRange(2,day.length - 1),1.5f)
            val daySpanColor = daySpanSize.toColorSpan(IntRange(2,day.length - 1),context.getColor(
                com.wika.basics.R.color.blue_1E90FF))
            holder.binding.tvCommRemain.text = daySpanColor
            holder.binding.tvCommRemain.setTextColor(context.getColor(R.color.black))
        }else if(remainDay == 0L){
            holder.binding.tvCommRemain.text = "就在今天"
            holder.binding.tvCommRemain.setTextColor(context.getColor(R.color.green_00C853))
        }else{
            holder.binding.tvCommRemain.text = "日期已过"
            holder.binding.tvCommRemain.setTextColor(context.getColor(R.color.red_DD2C00))
        }
        holder.binding.itemDelete.setOnClickListener {
            if(deleteCallBack != null){
                deleteCallBack?.onDelete(position,item)
            }
        }
    }

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ItemNoteBookBinding {
        return ItemNoteBookBinding.inflate(layoutInflater,parent,false)
    }

    override fun onItemMove(fromPosition: Int, toPosition: Int): Boolean {
        Collections.swap(getData(), fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        return true
    }

    override fun onItemDismiss(position: Int) {
    }

    fun addDeleteCallBack(callBack: OnDeleteCallBack){
        deleteCallBack = callBack
    }

    interface OnDeleteCallBack{
        fun onDelete(pos: Int,itemBean: NoteBookBean?)
    }
}
